package cn.xuexiluxian.open.common.utils.sms;

import cn.xuexiluxian.open.common.constant.Constants;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

public class TencentSmsUtils {
    public static final String secretId = "AKIDBBvObXCbjaAUZAZjeJisnSr1wb1B6lBL";
    public static final String secretKey = "uZx4vWs0sVt1vgCSSChwve9OO9fwxiDR";
    public static final String endpoint = "sms.tencentcloudapi.com";
    public static final String smsSdkAppId = "1400587931";

    public static void send(String templateId, String mobile, String ...datas){
        try{
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {mobile};
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setTemplateID(templateId);
            req.setSign(Constants.SIGNNAME);

            if(datas != null && datas.length > 0) {
                String[] templateParams = new String[datas.length];
                for (int i = 0; i < datas.length; i++) {
                    templateParams[i] = datas[i];
                }
                req.setTemplateParamSet(templateParams);
            }

            req.setSmsSdkAppid(smsSdkAppId);
            SendSmsResponse resp = client.SendSms(req);
            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}
